

//
//  ByViewIndexSearch.m
//  chequanbao_ios
//
//  Created by mc on 2017/11/23.
//  Copyright © 2017年 com.itboye. All rights reserved.
//

#import "ByViewIndexSearch.h"

@interface ByViewIndexSearch()<UITextFieldDelegate>
@end

@implementation ByViewIndexSearch
{
    UITextField     *_searchTf;
    UIButton        *_searchButton;
}

- (instancetype)initWithFrame:(CGRect)frame{
    if (self = [super initWithFrame:frame]) {
        [self createViews];
        self.backgroundColor = [UIColor colorWithWhite:0.5 alpha:0.25];
        self.layer.cornerRadius = self.height / 2;
    }
    return self;
}

- (void)createViews{
    CGFloat x,y,width,height;
    x = 15;
    y = 5;
    width = self.width - x * 2 - 20 - 10;
    height = 20;
    _searchTf = [[UITextField alloc] initWithFrame:CGRectMake(x, y, width, height)];
    [self addSubview:_searchTf];
    _searchTf.font = FONT(16);
    _searchTf.textColor = [UIColor whiteColor];
    _searchTf.tintColor = [UIColor whiteColor];  //设置光标颜色
    
    //修改占位符文字颜色
    NSMutableDictionary *attrs = [NSMutableDictionary dictionary];
    attrs[NSForegroundColorAttributeName] = [UIColor whiteColor];
    _searchTf.attributedPlaceholder = [[NSAttributedString alloc] initWithString:@"搜索" attributes:attrs];
    _searchTf.delegate = self;
    
    x = _searchTf.right + 10;
    width = height = 20;
    _searchButton = [[UIButton alloc] initWithFrame:CGRectMake(x, y, width, height)];
    [_searchButton setImage:[UIImage imageNamed:@"Index_search"] forState:UIControlStateNormal];
    [self addSubview:_searchButton];
    [_searchButton addTarget:self action:@selector(searchAction) forControlEvents:UIControlEventTouchUpInside];
    
}

- (void)searchAction{
    if (_searchTf.text.length != 0) {
        if (self.searchBlock) {
            self.searchBlock(_searchTf.text);
        }
        _searchTf.text = @"";
    }
}

- (void)textFieldDidEndEditing:(UITextField *)textField{
    [_searchTf resignFirstResponder];
}
@end
